package com.ah.shanxi_demo.convert;

import com.ah.shanxi_demo.common.enums.PolicyCommonEnum;
import com.ah.shanxi_demo.common.enums.PolicyDeleteEnum;
import com.ah.shanxi_demo.entity.SxPolicy;
import com.ah.shanxi_demo.param.result.PolicyRO;
import com.ah.shanxi_demo.sync.param.result.SyncPolicyResult;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zt 2020/8/19
 */
public class ConvertPolicy {

    /**
     * 策略列表转换
     *
     * @param sxPolicyList
     * @return
     */
    public static List<PolicyRO> convert_policy_ro_list(List<SxPolicy> sxPolicyList) {
        List<PolicyRO> list = new ArrayList<>();

        if (CollectionUtils.isEmpty(sxPolicyList)) {
            return new ArrayList<>();
        }
        sxPolicyList.stream().forEach(sxPolicy -> {
            list.add(convert_policy_ro(sxPolicy));
        });
        return list;
    }


    /**
     * 策略转换
     *
     * @param sxPolicy
     * @return
     */
    public static PolicyRO convert_policy_ro(SxPolicy sxPolicy) {

        PolicyRO model = new PolicyRO();
        BeanUtils.copyProperties(model, sxPolicy);

        return model;
    }

    /**
     * 部级平台返回策略数据 转换为表数据格式
     *
     * @param syncPolicyResults
     * @return
     */
    public static List<SxPolicy> convert_policy_list(List<SyncPolicyResult> syncPolicyResults, Long registerId) {

        List<SxPolicy> sxPolicyList=new ArrayList<>();

        syncPolicyResults.stream().forEach(p -> {

            SxPolicy sxPolicy=new SxPolicy();
            sxPolicy.setRegisterId(registerId);
            sxPolicy.setPolicyCode(p.getPolicy_code());
            sxPolicy.setGuidType(Integer.valueOf(p.getGuid_type()));
            sxPolicy.setGuid(p.getGuid());
            sxPolicy.setPolicyStatus(PolicyCommonEnum.STATUS_INIT.getKeyInt());
            sxPolicy.setDelete(PolicyDeleteEnum.UN_DELETE.getKeyInt());
            p.getPolicy_list().stream().forEach(item->{

                sxPolicy.setLogType(item.getLog_type());
                sxPolicy.setPolicyType(item.getPolicy_type());
                sxPolicy.setBeginTime(item.getBegin_time());
                sxPolicy.setEndTime(item.getEnd_time());

                sxPolicyList.add(sxPolicy);
            });
        });

        return sxPolicyList;
    }

}
